package com.leenmvc.core.exception;

import java.lang.reflect.InvocationTargetException;

/**
 * 持久层异常
 */
public class DaoException extends RuntimeException {

    /**
     * 是否展示给前端看
     */
    private boolean show = false;

    /**
     * 自定义的提示信息
     */
    private String message;

    /**
     * 异常信息
     */
    private Exception ex;

    /**
     * 执行那条sql语句的时候发生了异常
     */
    private String sql;

    public DaoException(String message, boolean show) {
        this.message = message;
        this.show = show;
    }

    public DaoException(Exception ex) {
        this.ex = ex;
        this.message = ex.getMessage();
        if(ex instanceof InvocationTargetException && ex.getMessage() == null) {
            this.message = ((InvocationTargetException)ex).getTargetException().getMessage();
        }
    }

    public DaoException(String message) {
        this.message = message;
    }

    public DaoException(String message, String sql) {
        this.message = message;
        this.sql = sql;
    }


    public boolean getShow() {
        return show;
    }

    /**
     *
     * @param message
     * @param sql
     * @param show
     */
    public DaoException(String message, String sql, boolean show) {
        this.message = message;
        this.sql = sql;
        this.show = show;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return message;
    }

    public boolean isShow() {
        return show;
    }

    public void setShow(boolean show) {
        this.show = show;
    }

    public Exception getEx() {
        return ex;
    }

    public void setEx(Exception ex) {
        this.ex = ex;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
}
